package top.ezzz.util.processtype;

import cn.hutool.system.oshi.OshiUtil;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import top.ezzz.model.ProcessInfo;

import java.util.ArrayList;
import java.util.List;

public class JavaProcess implements TypeInterface {
    @Override
    public List<ProcessInfo> getProcessList() {
        try {
            OperatingSystem system = OshiUtil.getOs();
            List<OSProcess> processes = system.getProcesses();
            List<ProcessInfo> result = new ArrayList<>();
            
            for (OSProcess process : processes) {
                try {
                    String name = process.getName();
                    if (name == null) continue;
                    
                    // 识别Java进程
                    if (!name.toLowerCase().contains("java")) {
                        continue;
                    }
                    
                    // 获取实时进程信息
                    result.add(new ProcessInfo()
                            .setPid(process.getProcessID())
                            .setName(name)
                            .setCommandLine(process.getCommandLine() != null ? process.getCommandLine() : "")
                            .setCpuUsage(0.0) // 暂时设为0，后续可以添加实时计算
                            .setMemoryUsage(process.getResidentSetSize())
                            .setStartTime(process.getStartTime())
                            .setUpTime(process.getUpTime())
                    );
                } catch (Exception e) {
                    // 跳过有问题的进程
                    continue;
                }
            }
            
            return result;
        } catch (Exception e) {
            // 如果获取进程列表失败，返回空列表
            return new ArrayList<>();
        }
    }
}
